Page({
  data: {
    voteList: [], // 用户创建的投票列表，包含投票的ID和标题等信息
    gradeList:[],
    projectList:[],
    haveVoteList: true,
    havGradeList:true,
    haveProjectList:true
  },
  onLoad(options) {
    if (wx.getStorageSync('userid') != '') {
     this.adjustVoteAndGradeOrder()
    } 

  },
  onPullDownRefresh() {
    this.adjustVoteAndGradeOrder()
  },
  getMyJoinedVoteListFromBlockChain() {
    let that = this
    return new Promise(function (resolve,reject) {
        wx.request({
            url: 'https://www.intohash.com/vote_backend/user/queryMyJoinedVote/' + wx.getStorageSync('userid'),
            method:"GET",
            success:function(res) {
                console.log(res)
                let votelist = []
                votelist = res.data.data.myJoinedVote
                that.setData({
                  voteList:votelist
                })
                resolve(votelist)
            },
            fail:function(res) {
              console.log(res)
              wx.showToast({
                title: '获取数据失败',
                icon: 'none'
              })
            }
          })
    })
   
  },
  getMyJoinedGradeListFromBlockChain(){
    let that = this
    return new Promise(function (reslove,reject) {
        wx.request({
            url: 'http://localhost:8010/vote_backend/user/queryMyJoinedGrade/'+wx.getStorageSync('userid'),
            method:"GET",
            success:function(res){
                console.log(res)
                let gradelist = []
                gradelist = res.data.data.myJoinedGrade
                that.setData({
                    gradeList:gradelist
                })
                reslove(gradelist)
            },
            fail:function(res) {
                console.log(res)
                wx.showToast({
                  title: '获取数据失败',
                  icon: 'none'
                })
              }
          })
    })
   
  },
  async adjustVoteAndGradeOrder(){
    //调整列表是否为空
    let voteList = await this.getMyJoinedVoteListFromBlockChain()
    let gradeList = await this.getMyJoinedGradeListFromBlockChain()
    let projectList = voteList.concat(gradeList)
    console.log(projectList)
    if(projectList.length==0){
        haveProjectList:false
    }
    projectList.sort((p1 , p2) =>{
        return (p2.voteId == null? p2.gradeId : p2.voteId) - (p1.voteId == null? p1.gradeId : p1.voteId);
    })
    this.setData({
        projectList:projectList
    })
  },
  onTapVote(e) {
    console.log(e);
    let projectList = this.data.projectList
    //是评分项目
    if(projectList[e.currentTarget.dataset.index].voteId == null){
        wx.navigateTo({
          url: '/pages/score/score?gradeID=' + projectList[e.currentTarget.dataset.index].gradeId + '&CreatedOrJoined=0',//0代表从我创建的页面进入
        })
    }
    //是投票项目
    else{
        wx.navigateTo({
            url: '/pages/vote/vote?voteID=' + projectList[e.currentTarget.dataset.index].voteId + '&CreatedOrJoined=0', //0代表从创建页面进入
        })
    }
  },
})